package com.kim.rssreader.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import cn.waps.AppConnect;

import com.kim.rssreader.R;
import com.kim.rssreader.activity.MainActivity;
import com.kim.rssreader.activity.Settings;
import com.kim.rssreader.activity.StarterActivity;

public class PopMenu extends AlertDialog implements View.OnTouchListener {

	TextView txv_setting;
	TextView txv_logout;
	TextView txv_ad;
	// TextView toMagazineBuy;

	View decorView = null;

	Context context;

	private int defaultColor = -1;
	private Drawable bg = null;

	private int gravity; // 重力
	private int animationStyle; // 进入、退出动画

	public PopMenu(Context context, int gravity, int animationStyle) {
		super(context);
		this.context = context;
		defaultColor = context.getResources().getColor(
				R.color.setting_background);
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.bg_menu_press);
		bg = new BitmapDrawable(bitmap);

		this.gravity = gravity;
		this.animationStyle = animationStyle;
	}

	public PopMenu(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.pop_magazine_menu);
		Window window = getWindow();

		// 设置dialog动画
		if (animationStyle != -1) {
			window.setWindowAnimations(animationStyle);
		}

		// 设置dialog属性
		window.setGravity(gravity /* Gravity.TOP | Gravity.RIGHT */);
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		lp.alpha = 1.0f;
		lp.dimAmount = 0.0f;
		window.setAttributes(lp);
		this.setCanceledOnTouchOutside(true);
		initView();
	}

	private void initView() {

		decorView = this.getWindow().getDecorView();

		txv_setting = (TextView) this.findViewById(R.id.mainactivity_settings);
		txv_logout = (TextView) this.findViewById(R.id.mainactivity_logout);
		txv_ad = (TextView) findViewById(R.id.mainactivity_ad);
		// toMagazineBuy = (TextView) this.findViewById(R.id.txt_magazine_buy);
		txv_setting.setOnTouchListener(this);
		txv_logout.setOnTouchListener(this);
		txv_ad.setOnTouchListener(this);
		// toMagazineBuy.setOnTouchListener(this);
		decorView.setOnTouchListener(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (MotionEvent.ACTION_UP == event.getAction()) {
			switch (v.getId()) {
			case R.id.mainactivity_settings:
				txv_setting.setBackgroundDrawable(null);
				txv_setting.setBackgroundColor(defaultColor);
				Intent intent = new Intent(context, Settings.class);
				context.startActivity(intent);
				break;
			case R.id.mainactivity_logout:
				txv_logout.setBackgroundDrawable(null);
				txv_logout.setBackgroundColor(defaultColor);
				StarterActivity.starterActivity.finish();
				MainActivity.mainActivity.finish();
				break;

			case R.id.mainactivity_ad:
				txv_ad.setBackgroundDrawable(null);
				txv_ad.setBackgroundColor(defaultColor);
				// 万普广告墙
				 AppConnect.getInstance(context).showOffers(context);

				// 有米广告墙
				// 推荐应用列表
				// YoumiOffersManager.showOffers(context,
				// YoumiOffersManager.TYPE_REWARDLESS_APPLIST);
				// 推荐单一应用
				// YoumiOffersManager.showOffers( context,
				// YoumiOffersManager.TYPE_REWARDLESS_FEATUREDAPP);

				break;

			}
			dismiss();
		} else if (MotionEvent.ACTION_DOWN == event.getAction()) {
			switch (v.getId()) {
			case R.id.mainactivity_settings:
				txv_setting.setBackgroundDrawable(bg);
				break;
			case R.id.mainactivity_logout:
				txv_logout.setBackgroundDrawable(bg);
				break;
			case R.id.mainactivity_ad:
				txv_ad.setBackgroundDrawable(bg);
				break;
			default:
				dismiss();
				break;
			}
		}
		return true;
	}

}
